import { Temp } from './types';
export const useTemp = defineStore({
  id: 'temp',
  state: () => <Temp>({
    deviceGroupList: [],
    fiveGAdeviceTypeList: [
      {
        id: 0,
        name: "5G-A基站",
      }, {
        id: 1,
        name: "雷达设备",
      }, {
        id: 2,
        name: "频谱设备",
      }, {
        id: 3,
        name: "光电设备",
      }, {
        id: 4,
        name: "反制设备",
      }, {
        id: 5,
        name: "诱骗设备",
      }, {
        id: 6,
        name: "干扰设备",
      }, {
        id: 7,
        name: "指挥车",
      }, {
        id: 8,
        name: "察打一体",
      }, {
        id: 999,
        name: "其他设备",
      }
    ],
    droneOPeratorList: [],
    droneModelList: {},
    audio: null,
    weather: {},
    isDeclared: false,
    dangerStatus: 0,
    noteList: [],
    model: false,
    visibleModal: false, // 弹窗警告
    modalList: []
  }),
  actions: {
    async getDeviceGroupList() {
      const { code, data } = await apiDeviceGroupList()
      if (code != 200) return
      this.deviceGroupList = data.list
    },
    async getNoteData() {
      const local = useLocal()
      const { code, data } = await apiNoticeList({
        uid: local.usermsg.id == 3 ? 0 : local.usermsg.id
      })
      if (code != 200 || !local.token) return;
      this.noteList = data.list
      // this.noteList = [
      //   {
      //     "name": "string",
      //     createTime: Date.now() / 1000,
      //     "uavId": "2051FEABPT0000000155",
      //     "lat": 31.018,
      //     "lng": 108.5352,
      //     "riskAreaId": 12,
      //     "uavType": 0,
      //     "status": 1,
      //     "snId": 0,
      //     "isRead": 0,
      //     "state": 0,
      //     "whiteUav": {
      //       "id": 0,
      //       "uavId": "2051FEABPT0000000155",
      //       "remark": "暂无",
      //       "status": 0,
      //       "createTime": 1733190108,
      //       "uid": "45",
      //       "uavNum": "渝45678",
      //       "customerName": "张三",
      //       "mobile": "15712345678",
      //       "address": "重庆万州xxx",
      //       "idCardNum": "411528198012214331",
      //       "color": "#f53f3f",
      //       "suffix": "个人",
      //       "typeName": "小型"
      //     },
      //     "startTime": 1733190083,
      //     "endTime": 1733190127
      //   }
      // ]
      if (!this.noteList?.length) return;
      if (!local.systemInfo?.isOpen) return;
      if (this.noteList[0].createTime > local.noticeCreateTime) {
        this.showModal(this.noteList[0])
        if (this.model) return;
        this.audio.play().then((res: any) => {
          local.noticeCreateTime = this.noteList[0].createTime
          console.log('播放成功', res)
        }).catch((err: any) => {
          console.log('播放失败', err)
          this.model = true
          useModal.warning({
            content: t('components.header.60ze4zm69xk1'),
            onBeforeOk: () => {
              local.systemInfo.isOpen = 1
              this.audio.play()
              this.model = false
              return true
            }
          })
        })
      }
    },
    showModal(e: any) {
      let statusArr = [1, 3]
      let redRiskAreaArr = [12, 13, 14, 15]
      let grayRiskAreaArr = [16, 18]
      if (!statusArr.includes(e.status) && (!redRiskAreaArr.includes(e.riskAreaId) || !grayRiskAreaArr.includes(e.riskAreaId))) return;
      if (e.whiteUav.suffix === "警用") return;
      if (e.whiteUav.suffix === "个人" && e.state) return;
      if (e.whiteUav.suffix === '单位' && e.state && (e.stratTime <= e.createTime || e.endTime >= e.createTime)) return;
      this.modalList.push(e)
      this.visibleModal = true;
    }
  }
})
